package co.edu.unal.satacredito.administration.view.monitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import co.edu.unal.satacredito.administration.view.general.AdministrationBean;
import co.edu.unal.satacredito.infrastructure.dto.ExternalServiceDTO;
import co.edu.unal.satacredito.infrastructure.dto.RealLoadDTO;
import co.edu.unal.satacredito.mediation.ExternalServicesEJBLocal;

@ManagedBean(name = "externalServicesBean")
@SessionScoped
public class ExternalServicesBean extends AdministrationBean {

	private String initDate;
	private String endDate;
	private List<ExternalServiceDTO> history;
	private List<ExternalServiceDTO> realTime;

	public String getInitDate() {
		return initDate;
	}

	public void setInitDate(String initDate) {
		this.initDate = initDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@EJB
	ExternalServicesEJBLocal externalServicesEJB;

	@PostConstruct
	public void init() {
		this.realTime = new ArrayList<>();
	}

	public String historic() {
		this.history = new ArrayList<>();
		Date initialDate = new Date(100, 00, 01);
		Date finalDate = new Date(130, 00, 01);
		if (!initDate.isEmpty())
			initialDate = new Date(initDate);
		if (!endDate.isEmpty())
			finalDate = new Date(endDate);
		finalDate.setHours(23);
		finalDate.setMinutes(59);
		finalDate.setSeconds(59);
		this.history = externalServicesEJB.getExternalServices(initialDate,
				finalDate);

		return SERVICES_HISTORIC_TABLE;
	}

	public String refresh() {
		ExternalServiceDTO e = new ExternalServiceDTO();
		e.setIpMachine("" + Math.random());
		List<ExternalServiceDTO> temp = externalServicesEJB
				.getServicesRealTime();
		// List<ExternalServiceDTO> temp = new ArrayList<>();
		// temp.add(e);
		for (ExternalServiceDTO externalServiceDTO : temp) {
			this.realTime.add(externalServiceDTO);
		}
		return REFRESH_SERVICES;
	}

	public static void clear() {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.remove("externalServicesBean");
	}

	public List<ExternalServiceDTO> getHistory() {
		return history;
	}

	public List<ExternalServiceDTO> getRealTime() {
		return realTime;
	}

}
